﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_roleAlter : BasePage
{
    int roleid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        if (!IsPostBack)
        {
            try
            {
                roleid = Convert.ToInt32(Request["id"]);
                Session["roleid"] = roleid;
            }
            catch 
            {
                UtilityMessageBox.ShowAndRedirect(this, "非法访问", "menuManager.aspx");
                return;
            }
            var m = RoleBLL.GetInstance().GetEntity(r => r.RoleId == roleid);
            rolename.Text = m.RoleName;
        }
        else
        {
            roleid = Convert.ToInt32(Session["roleid"]);
        }
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("roleManager.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string name = rolename.Text.Trim();
        if (name.Length == 0)
        {
            msg_name.InnerText = "角色名称不能为空";
            msg_name.Visible = true;
            return;
        }
        Role r = RoleBLL.GetInstance().GetEntity(k => k.RoleId != roleid && k.RoleName == name);
        if (r != null)
        {
            msg_name.InnerText = "角色名称已存在";
            msg_name.Visible = true;
            return;
        }
        Role role = RoleBLL.GetInstance().GetEntity(k => k.RoleId == roleid);
        role.RoleName = name;
        bool rr = RoleBLL.GetInstance().Update(role);
        if (rr)
        {
            UtilityMessageBox.ShowAndRedirect(this, "更新角色成功", "roleManager.aspx");
        }
        else
        {
            UtilityMessageBox.ShowAndRedirect(this, "更新角色失败", "roleManager.aspx");
        }
    }
}